#include "CharacterModellingForm.h"
#include "ui_CharacterModellingForm.h"
#include "CharacterItem.h"



CharacterModellingForm::CharacterModellingForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CharacterModellingForm)
{
    m_model = 0;
    ui->setupUi(this);

    initialize();

    on_descPushButton_clicked();
    addAction(ui->actionShow_Model);
}



CharacterModellingForm::~CharacterModellingForm()
{
    delete ui;
}



ItemModel* CharacterModellingForm::model() const
{
    return m_model;
}


void CharacterModellingForm::setModel(ItemModel *model)
{
    onClear();
    m_model = model;
    if (!m_model) return;

    m_model->setParent(this);
    ui->treeView->setModel(m_model);

    onRefresh();
}


void CharacterModellingForm::onClear()
{
    ui->campaignLineEdit->clear();
    ui->nameLineEdit->clear();
    ui->descTextEdit->clear();

}

void CharacterModellingForm::onRefresh()
{
    CharacterItem* root = dynamic_cast<CharacterItem*>(m_model->rootItem());
    if (!root) return;

    if (ui->campaignLineEdit->isReadOnly())
        ui->campaignLineEdit->setText(root->campaignName());
    else
        ui->campaignLineEdit->setText(root->campaignFile());

    ui->nameLineEdit->setText(root->name());
    ui->descTextEdit->setPlainText(root->desc());
}


void CharacterModellingForm::initialize()
{

}


void CharacterModellingForm::on_reloadPushButton_clicked()
{

}

void CharacterModellingForm::on_campaignPushButton_clicked()
{

}

void CharacterModellingForm::on_namePushButton_clicked()
{
    ui->nameLineEdit->setReadOnly(!ui->namePushButton->isChecked());
}

void CharacterModellingForm::on_descPushButton_clicked()
{
    ui->descTextEdit->setVisible(ui->descPushButton->isChecked());
}



void CharacterModellingForm::on_actionShow_Model_triggered()
{
    if (ui->stackedWidget->currentWidget() == ui->viewPage)
    {
        ui->stackedWidget->setCurrentWidget(ui->formPage);
        return;
    }
    ui->stackedWidget->setCurrentWidget(ui->viewPage);
}
